<?php
/*
Copyright 2008, Nathalia


Author:
Nathalia Sautchuk Patrício          (nathalia.sautchuk@gmail.com)

*/
class Home extends TPage
{
    public function onInit($param)
    {
        parent::onInit($param);
        if(!$this->IsPostBack)  // if the page is requested the first time
        {
            // populates post data into the repeater
            $this->populateData();
            // Retrieves the existing user data. This is equivalent to:
			// $userRecord=$this->getUserRecord();
            if($this->User->Name)
            {
			    $userRecord = $this->UserRecord;
                $this->Usuario->Text           = $userRecord->Nome; 
            }
        }      
			
    }

    public function validateUser($sender,$param)
	{
		$authManager=$this->Application->getModule('auth');
        if(!$authManager->login(strtolower($this->Login->Text),md5($this->Senha->Text)))
        {
            $param->IsValid=false;  // tell the validator that validation fails
        }
	}

	public function loginButtonClicked($sender,$param)
	{
		if($this->Page->IsValid)  // all validations succeed
        {
            // obtain the URL of the privileged page that the user wanted to visit originally
            $url=$this->Application->getModule('auth')->ReturnUrl;
            if(empty($url))  // the user accesses the login page directly
                $url=$this->Service->DefaultPageUrl;
            //$this->Response->redirect($url);
            $this->Response->redirect("./index.php?page=Home");
        }

	}
     
    protected function populateData()
    {
        $this->RepeaterSubmissoes->DataSource = $this->getCongressos('submissoes');
        $this->RepeaterSubmissoes->dataBind();

        $this->RepeaterEmAndamento->DataSource = $this->getCongressos('emAndamento');
        $this->RepeaterEmAndamento->dataBind();

        $this->RepeaterProximos->DataSource = $this->getCongressos('proximos');
        $this->RepeaterProximos->dataBind();

    }

    protected function getCongressos($caso)
    {
        // Construts a query criteria
        $criteria        = new TActiveRecordCriteria;
        $criteria->Limit = 3;

        //mostra próximos congressos com submissões abertas
        if($caso == 'submissoes')
        {
            $criteria->OrdersBy['DataSubmissaoTermino']='desc';  
            $criteria->Condition = "DataSubmissaoInicio < '".date('Y-m-d')."' AND DataSubmissaoTermino > '".date('Y-m-d')."'";
        }
        //mostra congressos em andamento, ocorrendo nesse momento
        else if($caso == 'emAndamento')  
        {      
            $criteria->OrdersBy['DataInicio']='desc';   
            $criteria->Condition = "DataInicio < '".date('Y-m-d')."' AND DataTermino > '".date('Y-m-d')."'";
        }
        //mostra proximos congressos
        else if($caso == 'proximos')
        {
            $criteria->OrdersBy['DataTermino']='desc'; 
            $criteria->Condition = "DataInicio > '".date('Y-m-d')."'";
        }
        // query for the posts with the above criteria and with author information
        return CongressoRecord::finder()->findAll($criteria);
    }

    protected function getUserRecord()
	{
		// if the 'username' GET var is not empty and the current user
		// is an administrator, we use the GET var value instead.

        if($this->User->Name!=='Guest')
        {
		    $usuarioID = $this->User->Name;

		    // use Active Record to look for the specified username
		    $usuarioRecord = UserRecord::finder()->findByUsuarioID($usuarioID);
		    if(!($usuarioRecord instanceof UserRecord))
			    throw new THttpException(500,'UsuarioID is invalid.');
		    return $usuarioRecord;
        }
	}
}
?>
